<html>
<head><meta charset="utf-8"><title>sleek derive(Error) · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/sleek.20derive(Error).html">sleek derive(Error)</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="156954322"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/sleek%20derive%28Error%29/near/156954322" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/sleek.20derive(Error).html#156954322">(Jan 27 2019 at 03:42)</a>:</h4>
<div class="codehilite"><pre><span></span><span class="cp">#[derive(Debug, MyError)]</span><span class="w"></span>
<span class="k">enum</span> <span class="nc">Error</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="cp">#[my_error::display(</span><span class="s">&quot;Could not open config file at {}: {}&quot;</span><span class="cp">, self.filename.display(), self.source)]</span><span class="w"></span>
<span class="w">    </span><span class="n">OpenConfig</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">filename</span>: <span class="nc">PathBuf</span><span class="p">,</span><span class="w"> </span><span class="n">source</span>: <span class="nc">io</span>::<span class="n">Error</span><span class="w"> </span><span class="p">},</span><span class="w"></span>
<span class="w">    </span><span class="n">SaveConfig</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">source</span>: <span class="nc">io</span>::<span class="n">Error</span><span class="w"> </span><span class="p">},</span><span class="w"></span>
<span class="w">    </span><span class="n">MissingUser</span><span class="p">,</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="k">type</span> <span class="nb">Result</span><span class="o">&lt;</span><span class="n">T</span><span class="p">,</span><span class="w"> </span><span class="n">E</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">Error</span><span class="o">&gt;</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">std</span>::<span class="n">result</span>::<span class="nb">Result</span><span class="o">&lt;</span><span class="n">T</span><span class="p">,</span><span class="w"> </span><span class="n">E</span><span class="o">&gt;</span><span class="p">;</span><span class="w"></span>

<span class="k">fn</span> <span class="nf">do_it</span><span class="p">(</span><span class="n">fail</span>: <span class="kt">bool</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="nb">Result</span><span class="o">&lt;</span><span class="p">()</span><span class="o">&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">filename</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s">&quot;/tmp/config&quot;</span><span class="p">;</span><span class="w"></span>

<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">_f</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">fs</span>::<span class="n">read</span><span class="p">(</span><span class="n">filename</span><span class="p">).</span><span class="n">context</span><span class="p">(</span><span class="n">OpenConfig</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">filename</span><span class="w"> </span><span class="p">})</span><span class="o">?</span><span class="p">;</span><span class="w"></span>
<span class="w">    </span><span class="n">fs</span>::<span class="n">write</span><span class="p">(</span><span class="n">filename</span><span class="p">,</span><span class="w"> </span><span class="s">&quot;config&quot;</span><span class="p">).</span><span class="n">context</span><span class="p">(</span><span class="n">SaveConfig</span><span class="p">)</span><span class="o">?</span><span class="p">;</span><span class="w"></span>

<span class="w">    </span><span class="k">if</span><span class="w"> </span><span class="n">fail</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="k">return</span><span class="w"> </span><span class="nb">Err</span><span class="p">(</span><span class="n">Error</span>::<span class="n">MissingUser</span><span class="p">);</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>

<span class="w">    </span><span class="nb">Ok</span><span class="p">(())</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</pre></div>


<p>Check out my sweet new Error-type-boilerplate-reduction-derive-macro thingie</p>



<a name="156954325"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/sleek%20derive%28Error%29/near/156954325" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/sleek.20derive(Error).html#156954325">(Jan 27 2019 at 03:43)</a>:</h4>
<p>And now to bed.</p>



<a name="156964942"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/sleek%20derive%28Error%29/near/156964942" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/sleek.20derive(Error).html#156964942">(Jan 27 2019 at 09:26)</a>:</h4>
<p>The failure crate has a very similar thing:</p>
<div class="codehilite"><pre><span></span><span class="c1">// taken from https://boats.gitlab.io/failure/</span>
<span class="cp">#[derive(Debug, Fail)]</span><span class="w"></span>
<span class="k">enum</span> <span class="nc">ToolchainError</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="cp">#[fail(display = </span><span class="s">&quot;invalid toolchain name: {}&quot;</span><span class="cp">, name)]</span><span class="w"></span>
<span class="w">    </span><span class="n">InvalidToolchainName</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="n">name</span>: <span class="nb">String</span><span class="p">,</span><span class="w"></span>
<span class="w">    </span><span class="p">},</span><span class="w"></span>
<span class="w">    </span><span class="cp">#[fail(display = </span><span class="s">&quot;unknown toolchain version: {}&quot;</span><span class="cp">, version)]</span><span class="w"></span>
<span class="w">    </span><span class="n">UnknownToolchainVersion</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="n">version</span>: <span class="nb">String</span><span class="p">,</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</pre></div>


<p>is there a major difference here?</p>



<a name="156974670"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/sleek%20derive%28Error%29/near/156974670" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/sleek.20derive(Error).html#156974670">(Jan 27 2019 at 14:49)</a>:</h4>
<p>Yes: I didn't invent a brand new trait. Failure doesn't implement <code>From</code> AFAICT.</p>



<a name="156974674"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/sleek%20derive%28Error%29/near/156974674" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/sleek.20derive(Error).html#156974674">(Jan 27 2019 at 14:49)</a>:</h4>
<p>There's a reason I also included the usage sites</p>



<a name="156974729"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/sleek%20derive%28Error%29/near/156974729" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/sleek.20derive(Error).html#156974729">(Jan 27 2019 at 14:50)</a>:</h4>
<p>Failure's Display also <a href="https://github.com/rust-lang-nursery/failure/issues/189" target="_blank" title="https://github.com/rust-lang-nursery/failure/issues/189">forces the causal error to be printed in a certain way</a></p>



<a name="156974781"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/sleek%20derive%28Error%29/near/156974781" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/sleek.20derive(Error).html#156974781">(Jan 27 2019 at 14:52)</a>:</h4>
<p>Every time I've attempted to use failure (not many, TBH), I've felt that it wasn't designed by folk that actually used it.</p>
<p>Obviously that can't be true, but I've never once understood why people are so very in love with it.</p>



<a name="156974845"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/sleek%20derive%28Error%29/near/156974845" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/sleek.20derive(Error).html#156974845">(Jan 27 2019 at 14:54)</a>:</h4>
<p>One thing that failure states (obliquely) that I agree with is that directly supporting <code>From&lt;AnotherError&gt;</code> isn't a great pattern. Just because an IO error happened doesn't express anything useful. There's a context to that IO operation in my usage (in the example: reading or writing a file)</p>



<a name="156974856"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/sleek%20derive%28Error%29/near/156974856" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/sleek.20derive(Error).html#156974856">(Jan 27 2019 at 14:55)</a>:</h4>
<p>Which is why I like <code>fs::read(filename).context(OpenConfig { filename })?</code></p>



<a name="156974906"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/sleek%20derive%28Error%29/near/156974906" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/sleek.20derive(Error).html#156974906">(Jan 27 2019 at 14:56)</a>:</h4>
<p>extending the current concept of "context" to mean both "some additional data beyond just the error type" and "the context of <strong>my</strong> code in which the underlying error happened"</p>



<a name="156975019"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/sleek%20derive%28Error%29/near/156975019" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/sleek.20derive(Error).html#156975019">(Jan 27 2019 at 15:00)</a>:</h4>
<p>That’s what I do with "error chaining"</p>



<a name="156975036"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/sleek%20derive%28Error%29/near/156975036" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/sleek.20derive(Error).html#156975036">(Jan 27 2019 at 15:00)</a>:</h4>
<p>(adding context)</p>



<a name="156975042"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/sleek%20derive%28Error%29/near/156975042" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/sleek.20derive(Error).html#156975042">(Jan 27 2019 at 15:00)</a>:</h4>
<p>I even began using this concept in C++ and errors there, implementing casts and constructors between different kinds of errors to collect the context.</p>



<a name="156977813"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/sleek%20derive%28Error%29/near/156977813" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/sleek.20derive(Error).html#156977813">(Jan 27 2019 at 16:27)</a>:</h4>
<p>Do you use failure? If so, can you provide an equivalent construct? I could not figure out how to do it.</p>



<a name="156977935"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/sleek%20derive%28Error%29/near/156977935" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Pietro Albini <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/sleek.20derive(Error).html#156977935">(Jan 27 2019 at 16:30)</a>:</h4>
<p>do you mean</p>
<div class="codehilite"><pre><span></span><span class="n">fs</span>::<span class="n">read</span><span class="p">(</span><span class="n">filename</span><span class="p">).</span><span class="n">with_context</span><span class="p">(</span><span class="o">|</span><span class="n">_</span><span class="o">|</span><span class="w"> </span><span class="n">OpenConfig</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">filename</span><span class="w"> </span><span class="p">})</span><span class="o">?</span><span class="w"></span>
</pre></div>


<p>?</p>



<a name="156977953"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/sleek%20derive%28Error%29/near/156977953" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/sleek.20derive(Error).html#156977953">(Jan 27 2019 at 16:31)</a>:</h4>
<p>I may have used <code>failure</code> a while ago, not sure in which project though. IIRC what I would usually do is <code>fallible().context(format!("my nice error message and some formatted useful info")</code> IIRC.</p>



<a name="156977997"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/sleek%20derive%28Error%29/near/156977997" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/sleek.20derive(Error).html#156977997">(Jan 27 2019 at 16:32)</a>:</h4>
<p>ah it was internal work project IIRC.</p>



<a name="156978000"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/sleek%20derive%28Error%29/near/156978000" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/sleek.20derive(Error).html#156978000">(Jan 27 2019 at 16:32)</a>:</h4>
<p>can’t check the code there now</p>



<a name="156978001"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/sleek%20derive%28Error%29/near/156978001" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Pietro Albini <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/sleek.20derive(Error).html#156978001">(Jan 27 2019 at 16:32)</a>:</h4>
<p>with failure (on crater) I just use <code>with_context</code> everywhere</p>



<a name="156978004"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/sleek%20derive%28Error%29/near/156978004" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/sleek.20derive(Error).html#156978004">(Jan 27 2019 at 16:32)</a>:</h4>
<p>So that'd be using stringly-typed errors, yes? Maybe OK for an app, not the best for a library?</p>



<a name="156978006"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/sleek%20derive%28Error%29/near/156978006" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Pietro Albini <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/sleek.20derive(Error).html#156978006">(Jan 27 2019 at 16:32)</a>:</h4>
<p>to avoid slowing down things where stuff is ok</p>



<a name="156978007"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/sleek%20derive%28Error%29/near/156978007" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Pietro Albini <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/sleek.20derive(Error).html#156978007">(Jan 27 2019 at 16:33)</a>:</h4>
<p>you can of course use typed errors as contexts</p>



<a name="156978017"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/sleek%20derive%28Error%29/near/156978017" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Pietro Albini <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/sleek.20derive(Error).html#156978017">(Jan 27 2019 at 16:33)</a>:</h4>
<p>on crater I don't need them to be typed though</p>



<a name="156978018"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/sleek%20derive%28Error%29/near/156978018" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/sleek.20derive(Error).html#156978018">(Jan 27 2019 at 16:33)</a>:</h4>
<p>yeah, in my case I was making an executable as well (rather than a library)</p>



<a name="156978019"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/sleek%20derive%28Error%29/near/156978019" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/sleek.20derive(Error).html#156978019">(Jan 27 2019 at 16:34)</a>:</h4>
<p>for libraries I tend to avoid any error handling crates entirely</p>



<a name="156978061"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/sleek%20derive%28Error%29/near/156978061" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/sleek.20derive(Error).html#156978061">(Jan 27 2019 at 16:34)</a>:</h4>
<p>Why?</p>



<a name="156978067"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/sleek%20derive%28Error%29/near/156978067" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/sleek.20derive(Error).html#156978067">(Jan 27 2019 at 16:34)</a>:</h4>
<p>No idea... my libraries are all on the small side and not pulling in such a crate feels more lightweight</p>



<a name="156978069"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/sleek%20derive%28Error%29/near/156978069" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/sleek.20derive(Error).html#156978069">(Jan 27 2019 at 16:35)</a>:</h4>
<p>I guess.</p>



<a name="156978084"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/sleek%20derive%28Error%29/near/156978084" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/sleek.20derive(Error).html#156978084">(Jan 27 2019 at 16:35)</a>:</h4>
<p><span class="user-mention" data-user-id="121055">@Pietro Albini</span> how would you create those typed contexts? More applications of deriving <code>Fail</code>?</p>



<a name="156978095"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/sleek%20derive%28Error%29/near/156978095" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Pietro Albini <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/sleek.20derive(Error).html#156978095">(Jan 27 2019 at 16:35)</a>:</h4>
<p>yep, mostly the same as your derive</p>



<a name="156978098"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/sleek%20derive%28Error%29/near/156978098" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Pietro Albini <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/sleek.20derive(Error).html#156978098">(Jan 27 2019 at 16:35)</a>:</h4>
<p>one sec</p>



<a name="156978142"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/sleek%20derive%28Error%29/near/156978142" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Pietro Albini <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/sleek.20derive(Error).html#156978142">(Jan 27 2019 at 16:36)</a>:</h4>
<p><a href="https://github.com/rust-lang-nursery/crater/blob/master/src/utils/hex.rs" target="_blank" title="https://github.com/rust-lang-nursery/crater/blob/master/src/utils/hex.rs">https://github.com/rust-lang-nursery/crater/blob/master/src/utils/hex.rs</a> for example</p>



<a name="156978213"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/sleek%20derive%28Error%29/near/156978213" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/sleek.20derive(Error).html#156978213">(Jan 27 2019 at 16:38)</a>:</h4>
<p>... you wrote your own hex decoding?</p>



<a name="156978219"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/sleek%20derive%28Error%29/near/156978219" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Pietro Albini <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/sleek.20derive(Error).html#156978219">(Jan 27 2019 at 16:39)</a>:</h4>
<p>didn't want to pull a crate just for 30 lines of code :(</p>



<a name="156978229"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/sleek%20derive%28Error%29/near/156978229" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Pietro Albini <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/sleek.20derive(Error).html#156978229">(Jan 27 2019 at 16:39)</a>:</h4>
<p>crater already has something like 300 deps</p>



<a name="156978231"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/sleek%20derive%28Error%29/near/156978231" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Pietro Albini <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/sleek.20derive(Error).html#156978231">(Jan 27 2019 at 16:39)</a>:</h4>
<p>(counting deps of the deps)</p>



<a name="156978232"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/sleek%20derive%28Error%29/near/156978232" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/sleek.20derive(Error).html#156978232">(Jan 27 2019 at 16:39)</a>:</h4>
<p>I shan't ever understand you folk.</p>



<a name="156978234"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/sleek%20derive%28Error%29/near/156978234" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/sleek.20derive(Error).html#156978234">(Jan 27 2019 at 16:39)</a>:</h4>
<p>One of those is probably the hex crate... If so, using the dependency would make compiling <em>faster</em></p>



<a name="156978287"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/sleek%20derive%28Error%29/near/156978287" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/sleek.20derive(Error).html#156978287">(Jan 27 2019 at 16:40)</a>:</h4>
<p>Looks like 60 lines because you wrote tests, because it wasn't tested by other people</p>



<a name="156978300"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/sleek%20derive%28Error%29/near/156978300" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/sleek.20derive(Error).html#156978300">(Jan 27 2019 at 16:41)</a>:</h4>
<p>Anyway, I'm missing how this example corresponds to my code above.</p>



<a name="156978354"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/sleek%20derive%28Error%29/near/156978354" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/sleek.20derive(Error).html#156978354">(Jan 27 2019 at 16:42)</a>:</h4>
<p>Let me start over, please.</p>



<a name="156978360"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/sleek%20derive%28Error%29/near/156978360" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/sleek.20derive(Error).html#156978360">(Jan 27 2019 at 16:42)</a>:</h4>
<div class="codehilite"><pre><span></span><span class="kd">let</span><span class="w"> </span><span class="n">_f</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">fs</span>::<span class="n">read</span><span class="p">(</span><span class="n">filename</span><span class="p">).</span><span class="n">context</span><span class="p">(</span><span class="n">OpenConfig</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">filename</span><span class="w"> </span><span class="p">})</span><span class="o">?</span><span class="p">;</span><span class="w"></span>
<span class="n">fs</span>::<span class="n">write</span><span class="p">(</span><span class="n">filename</span><span class="p">,</span><span class="w"> </span><span class="s">&quot;config&quot;</span><span class="p">).</span><span class="n">context</span><span class="p">(</span><span class="n">SaveConfig</span><span class="p">)</span><span class="o">?</span><span class="p">;</span><span class="w"></span>
</pre></div>


<p>Can you show the equivalent using failure? i.e. "this <code>io::Error</code> means something went bad for opening a file, this <code>io::Error</code> means something when bad saving a file"</p>



<a name="156978369"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/sleek%20derive%28Error%29/near/156978369" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/sleek.20derive(Error).html#156978369">(Jan 27 2019 at 16:43)</a>:</h4>
<p>Which would get bucketed into </p>
<div class="codehilite"><pre><span></span><span class="k">enum</span> <span class="nc">Error</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="n">OpenConfig</span><span class="p">,</span><span class="w"> </span><span class="c1">// whatever you need, presumably an io::Error is inside here</span>
<span class="w">    </span><span class="n">SaveConfig</span><span class="p">,</span><span class="w"> </span><span class="c1">// whatever you need, presumably an io::Error is inside here</span>
<span class="p">}</span><span class="w"></span>
</pre></div>



<a name="156978372"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/sleek%20derive%28Error%29/near/156978372" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Pietro Albini <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/sleek.20derive(Error).html#156978372">(Jan 27 2019 at 16:43)</a>:</h4>
<div class="codehilite"><pre><span></span><span class="kd">let</span><span class="w"> </span><span class="n">_f</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">fs</span>::<span class="n">read</span><span class="p">(</span><span class="n">filename</span><span class="p">).</span><span class="n">with_context</span><span class="p">(</span><span class="o">|</span><span class="n">_</span><span class="o">|</span><span class="w"> </span><span class="n">ConfigError</span>::<span class="n">OpenConfig</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">filename</span><span class="w"> </span><span class="p">})</span><span class="o">?</span><span class="p">;</span><span class="w"></span>
<span class="n">fs</span>::<span class="n">write</span><span class="p">(</span><span class="n">filename</span><span class="p">,</span><span class="w"> </span><span class="s">&quot;config&quot;</span><span class="p">).</span><span class="n">context</span><span class="p">(</span><span class="n">ConfigError</span>::<span class="n">SaveConfig</span><span class="p">)</span><span class="o">?</span><span class="p">;</span><span class="w"></span>
</pre></div>



<a name="156978423"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/sleek%20derive%28Error%29/near/156978423" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Pietro Albini <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/sleek.20derive(Error).html#156978423">(Jan 27 2019 at 16:44)</a>:</h4>
<p>and the context definition is the same one you would use for an error</p>



<a name="156978426"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/sleek%20derive%28Error%29/near/156978426" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Pietro Albini <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/sleek.20derive(Error).html#156978426">(Jan 27 2019 at 16:44)</a>:</h4>
<p>for example that hex error enum</p>



<a name="156978430"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/sleek%20derive%28Error%29/near/156978430" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/sleek.20derive(Error).html#156978430">(Jan 27 2019 at 16:45)</a>:</h4>
<p>what does the error type (an enum?) that contains either of <code>OpenConfig</code> / <code>SaveConfig</code> look like?</p>



<a name="156978501"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/sleek%20derive%28Error%29/near/156978501" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Pietro Albini <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/sleek.20derive(Error).html#156978501">(Jan 27 2019 at 16:46)</a>:</h4>
<div class="codehilite"><pre><span></span><span class="cp">#[derive(Debug, Fail)]</span><span class="w"></span>
<span class="k">enum</span> <span class="nc">ConfigError</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="cp">#[fail(display = </span><span class="s">&quot;failed to open configuration file {}&quot;</span><span class="cp">, filename)]</span><span class="w"></span>
<span class="w">    </span><span class="n">OpenConfig</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">filename</span>: <span class="nb">String</span> <span class="p">},</span><span class="w"></span>
<span class="w">    </span><span class="cp">#[fail(display = </span><span class="s">&quot;failed to save the configuration file&quot;</span><span class="cp">)]</span><span class="w"></span>
<span class="w">    </span><span class="n">SaveConfig</span><span class="p">,</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</pre></div>



<a name="156978637"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/sleek%20derive%28Error%29/near/156978637" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/sleek.20derive(Error).html#156978637">(Jan 27 2019 at 16:50)</a>:</h4>
<p>Now I'm missing something... how does the struct <code>OpenConfig</code> (which is also <code>derive(Fail)</code>, right?) get converted to <code>ConfigError::OpenConfig</code>?</p>



<a name="156978651"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/sleek%20derive%28Error%29/near/156978651" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/sleek.20derive(Error).html#156978651">(Jan 27 2019 at 16:51)</a>:</h4>
<p>and where does the underlying cause (the <code>io::Error</code>) get stashed?</p>



<a name="156978714"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/sleek%20derive%28Error%29/near/156978714" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Pietro Albini <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/sleek.20derive(Error).html#156978714">(Jan 27 2019 at 16:52)</a>:</h4>
<blockquote>
<p>Now I'm missing something... how does the struct OpenConfig (which is also derive(Fail), right?) get converted to ConfigError::OpenConfig?</p>
</blockquote>
<p>uh... it's actually <code>ConfigError::OpenConfig</code>, woops</p>



<a name="156978722"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/sleek%20derive%28Error%29/near/156978722" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/sleek.20derive(Error).html#156978722">(Jan 27 2019 at 16:53)</a>:</h4>
<p>So is is actually using <code>with_context</code> then?</p>



<a name="156978733"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/sleek%20derive%28Error%29/near/156978733" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/sleek.20derive(Error).html#156978733">(Jan 27 2019 at 16:53)</a>:</h4>
<p>cause a <code>Context&lt;io:Error, ConfigError&gt;</code> presumably cannot be converted to a <code>ConfigError</code>...</p>



<a name="156978780"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/sleek%20derive%28Error%29/near/156978780" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Pietro Albini <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/sleek.20derive(Error).html#156978780">(Jan 27 2019 at 16:54)</a>:</h4>
<p>yep, but it can be converted to a <code>Box&lt;Fail&gt;</code> or <code>Error</code></p>



<a name="156978798"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/sleek%20derive%28Error%29/near/156978798" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Pietro Albini <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/sleek.20derive(Error).html#156978798">(Jan 27 2019 at 16:54)</a>:</h4>
<p>you can implement contexts manually if you want to avoid the boxing</p>



<a name="156978838"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/sleek%20derive%28Error%29/near/156978838" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Pietro Albini <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/sleek.20derive(Error).html#156978838">(Jan 27 2019 at 16:55)</a>:</h4>
<p>never did that, but <em>I think</em> you can do something like</p>
<div class="codehilite"><pre><span></span><span class="cp">#[derive(Debug, Fail)]</span><span class="w"></span>
<span class="k">enum</span> <span class="nc">ConfigError</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="cp">#[fail(display = </span><span class="s">&quot;failed to open configuration file {}&quot;</span><span class="cp">, filename)]</span><span class="w"></span>
<span class="w">    </span><span class="n">OpenConfig</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">filename</span>: <span class="nb">String</span><span class="p">,</span><span class="w"> </span><span class="cp">#[cause]</span><span class="w"> </span><span class="n">cause</span>: <span class="nc">io</span>::<span class="n">Error</span><span class="w"> </span><span class="p">},</span><span class="w"></span>
<span class="w">    </span><span class="cp">#[fail(display = </span><span class="s">&quot;failed to save the configuration file&quot;</span><span class="cp">)]</span><span class="w"></span>
<span class="w">    </span><span class="n">SaveConfig</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="cp">#[cause]</span><span class="w"> </span><span class="n">cause</span>: <span class="nc">io</span>::<span class="n">Error</span><span class="w"> </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</pre></div>



<a name="156978884"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/sleek%20derive%28Error%29/near/156978884" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Pietro Albini <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/sleek.20derive(Error).html#156978884">(Jan 27 2019 at 16:56)</a>:</h4>
<p>you won't be able to use the <code>.with_context</code> method though</p>



<a name="156978965"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/sleek%20derive%28Error%29/near/156978965" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/sleek.20derive(Error).html#156978965">(Jan 27 2019 at 16:58)</a>:</h4>
<p>which also means you can't use <code>?</code>, right?</p>



<a name="156978967"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/sleek%20derive%28Error%29/near/156978967" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Pietro Albini <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/sleek.20derive(Error).html#156978967">(Jan 27 2019 at 16:58)</a>:</h4>
<p>no</p>



<a name="156978982"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/sleek%20derive%28Error%29/near/156978982" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/sleek.20derive(Error).html#156978982">(Jan 27 2019 at 16:59)</a>:</h4>
<p>What's the equivalent of <code>fs::read(filename).context(OpenConfig { filename })?</code> for this most recent enum?</p>



<a name="156979045"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/sleek%20derive%28Error%29/near/156979045" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Pietro Albini <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/sleek.20derive(Error).html#156979045">(Jan 27 2019 at 17:00)</a>:</h4>
<div class="codehilite"><pre><span></span><span class="n">fs</span>::<span class="n">read</span><span class="p">(</span><span class="n">filename</span><span class="p">).</span><span class="n">map_err</span><span class="p">(</span><span class="o">|</span><span class="n">io</span><span class="o">|</span><span class="w"> </span><span class="n">ConfigError</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">filename</span><span class="p">,</span><span class="w"> </span><span class="n">cause</span>: <span class="nc">io</span><span class="w"> </span><span class="p">})</span><span class="o">?</span><span class="p">;</span><span class="w"></span>
</pre></div>



<a name="156979047"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/sleek%20derive%28Error%29/near/156979047" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Pietro Albini <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/sleek.20derive(Error).html#156979047">(Jan 27 2019 at 17:00)</a>:</h4>
<p><em>I think</em></p>



<a name="156979059"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/sleek%20derive%28Error%29/near/156979059" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Pietro Albini <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/sleek.20derive(Error).html#156979059">(Jan 27 2019 at 17:00)</a>:</h4>
<p>haven't used an enum like that though</p>



<a name="156979142"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/sleek%20derive%28Error%29/near/156979142" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/sleek.20derive(Error).html#156979142">(Jan 27 2019 at 17:02)</a>:</h4>
<p>so failure isn't providing us anything for that line, as that's just a struct literal and <code>Result::map_err</code></p>



<a name="156979153"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/sleek%20derive%28Error%29/near/156979153" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/sleek.20derive(Error).html#156979153">(Jan 27 2019 at 17:03)</a>:</h4>
<p>Also, you totally got to write it as <code>map_err(|cause| ConfigError { filename, cause })?;</code> <span aria-label="wink" class="emoji emoji-1f609" role="img" title="wink">:wink:</span></p>



<a name="156979156"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/sleek%20derive%28Error%29/near/156979156" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Pietro Albini <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/sleek.20derive(Error).html#156979156">(Jan 27 2019 at 17:03)</a>:</h4>
<p>yeah, if you want <code>.context</code> you have to use the provided <code>Context</code> struct</p>



<a name="156979200"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/sleek%20derive%28Error%29/near/156979200" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/sleek.20derive(Error).html#156979200">(Jan 27 2019 at 17:04)</a>:</h4>
<p>compare:</p>
<div class="codehilite"><pre><span></span><span class="c1">// failure</span>
<span class="n">foo</span><span class="p">().</span><span class="n">map_err</span><span class="p">(</span><span class="o">|</span><span class="n">cause</span><span class="o">|</span><span class="w"> </span><span class="n">ConfigError</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">filename</span><span class="p">,</span><span class="w"> </span><span class="n">cause</span><span class="w"> </span><span class="p">})</span><span class="o">?</span><span class="p">;</span><span class="w"></span>
<span class="c1">// to-be-named</span>
<span class="n">foo</span><span class="p">().</span><span class="n">context</span><span class="p">(</span><span class="n">OpenConfig</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">filename</span><span class="w"> </span><span class="p">})</span><span class="o">?</span><span class="p">;</span><span class="w"></span>
</pre></div>



<a name="156979201"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/sleek%20derive%28Error%29/near/156979201" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Pietro Albini <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/sleek.20derive(Error).html#156979201">(Jan 27 2019 at 17:04)</a>:</h4>
<p>I'm technically doing other things right now <span aria-label="stuck out tongue" class="emoji emoji-1f61d" role="img" title="stuck out tongue">:stuck_out_tongue:</span></p>



<a name="156979208"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/sleek%20derive%28Error%29/near/156979208" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Pietro Albini <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/sleek.20derive(Error).html#156979208">(Jan 27 2019 at 17:04)</a>:</h4>
<p>I'd prefer if context was a closure though</p>



<a name="156979210"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/sleek%20derive%28Error%29/near/156979210" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Pietro Albini <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/sleek.20derive(Error).html#156979210">(Jan 27 2019 at 17:04)</a>:</h4>
<p>or at least support closures there</p>



<a name="156979223"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/sleek%20derive%28Error%29/near/156979223" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Pietro Albini <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/sleek.20derive(Error).html#156979223">(Jan 27 2019 at 17:05)</a>:</h4>
<p>and how do you store the underlying cause?</p>



<a name="156979229"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/sleek%20derive%28Error%29/near/156979229" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/sleek.20derive(Error).html#156979229">(Jan 27 2019 at 17:05)</a>:</h4>
<p>I mean, that's a straight-forward addition, and orthogonal. Failure supports both (<code>context</code> / <code>with_context</code>)</p>



<a name="156979235"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/sleek%20derive%28Error%29/near/156979235" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/sleek.20derive(Error).html#156979235">(Jan 27 2019 at 17:05)</a>:</h4>
<p>(and I added it to my TODO, as well)</p>



<a name="156979278"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/sleek%20derive%28Error%29/near/156979278" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/sleek.20derive(Error).html#156979278">(Jan 27 2019 at 17:06)</a>:</h4>
<div class="codehilite"><pre><span></span><span class="cp">#[derive(Debug, MyError)]</span><span class="w"></span>
<span class="k">enum</span> <span class="nc">Error</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="n">OpenConfig</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">filename</span>: <span class="nc">PathBuf</span><span class="p">,</span><span class="w"> </span><span class="n">source</span>: <span class="nc">io</span>::<span class="n">Error</span><span class="w"> </span><span class="p">},</span><span class="w"></span>
<span class="w">    </span><span class="n">SaveConfig</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">source</span>: <span class="nc">io</span>::<span class="n">Error</span><span class="w"> </span><span class="p">},</span><span class="w"></span>
</pre></div>



<a name="156979282"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/sleek%20derive%28Error%29/near/156979282" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/sleek.20derive(Error).html#156979282">(Jan 27 2019 at 17:06)</a>:</h4>
<p>The cause is right there ^</p>



<a name="156979285"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/sleek%20derive%28Error%29/near/156979285" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/sleek.20derive(Error).html#156979285">(Jan 27 2019 at 17:06)</a>:</h4>
<p>using that fresh new name of "source"</p>



<a name="156979307"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/sleek%20derive%28Error%29/near/156979307" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Pietro Albini <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/sleek.20derive(Error).html#156979307">(Jan 27 2019 at 17:07)</a>:</h4>
<p>so you have a struct <code>OpenConfig</code> that you then convert to <code>Error::OpenConfig</code>?</p>



<a name="156979311"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/sleek%20derive%28Error%29/near/156979311" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/sleek.20derive(Error).html#156979311">(Jan 27 2019 at 17:07)</a>:</h4>
<p>you are correct</p>



<a name="156979315"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/sleek%20derive%28Error%29/near/156979315" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Pietro Albini <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/sleek.20derive(Error).html#156979315">(Jan 27 2019 at 17:07)</a>:</h4>
<p>hmm</p>



<a name="156979317"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/sleek%20derive%28Error%29/near/156979317" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Pietro Albini <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/sleek.20derive(Error).html#156979317">(Jan 27 2019 at 17:07)</a>:</h4>
<p>could be nice</p>



<a name="156979318"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/sleek%20derive%28Error%29/near/156979318" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/sleek.20derive(Error).html#156979318">(Jan 27 2019 at 17:07)</a>:</h4>
<p>(it's been pointed out that a <em>derive</em> creating new types is not an obvious use, so I'll probably switch to a plain proc-macro)</p>



<a name="156979363"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/sleek%20derive%28Error%29/near/156979363" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/sleek.20derive(Error).html#156979363">(Jan 27 2019 at 17:08)</a>:</h4>
<p>And of course, this all needs docs with prose</p>



<a name="156979367"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/sleek%20derive%28Error%29/near/156979367" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/sleek.20derive(Error).html#156979367">(Jan 27 2019 at 17:08)</a>:</h4>
<p>But pasting it here has helped me figure out the "value proposition" as well as what needs to be explicitly pointed out</p>



<a name="156979376"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/sleek%20derive%28Error%29/near/156979376" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/sleek.20derive(Error).html#156979376">(Jan 27 2019 at 17:09)</a>:</h4>
<p>And prevents me from releasing purely duplicate crates ;-)</p>



<a name="156979380"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/sleek%20derive%28Error%29/near/156979380" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/sleek.20derive(Error).html#156979380">(Jan 27 2019 at 17:09)</a>:</h4>
<p>Before I forget, thank you both <span class="user-mention" data-user-id="123586">@nagisa</span> and <span class="user-mention" data-user-id="121055">@Pietro Albini</span> !</p>



<a name="156979429"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/sleek%20derive%28Error%29/near/156979429" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Pietro Albini <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/sleek.20derive(Error).html#156979429">(Jan 27 2019 at 17:10)</a>:</h4>
<p>is this going to be the third error handling crate on the nursery?</p>



<a name="156979431"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/sleek%20derive%28Error%29/near/156979431" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/sleek.20derive(Error).html#156979431">(Jan 27 2019 at 17:10)</a>:</h4>
<p>I also need to add some backtrace. Debating over </p>
<div class="codehilite"><pre><span></span><span class="k">enum</span> <span class="nc">Error</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">   </span><span class="cp">#[my_error::backtrace]</span><span class="w"></span>
<span class="w">   </span><span class="n">Alpha</span><span class="w"></span>
<span class="w">   </span><span class="c1">// vs</span>
<span class="w">   </span><span class="n">Alpha</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">backtrace</span>: <span class="nc">my_error</span>::<span class="n">Backtrace</span><span class="w"> </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</pre></div>



<a name="156979448"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/sleek%20derive%28Error%29/near/156979448" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/sleek.20derive(Error).html#156979448">(Jan 27 2019 at 17:11)</a>:</h4>
<p>I don't know why it'd be on the nursery; most of the crates I've made have faded into obscurity. I don't have the position of authority to claim that my crate will replace the standard library and everyone should jump ship to it. <span aria-label="wink" class="emoji emoji-1f609" role="img" title="wink">:wink:</span></p>



<a name="156979453"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/sleek%20derive%28Error%29/near/156979453" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Pietro Albini <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/sleek.20derive(Error).html#156979453">(Jan 27 2019 at 17:12)</a>:</h4>
<p>what about</p>
<div class="codehilite"><pre><span></span>enum Error {
    Alpha { ctx: Ctx&lt;io::Error&gt; }
}
</pre></div>



<a name="156979497"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/sleek%20derive%28Error%29/near/156979497" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Pietro Albini <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/sleek.20derive(Error).html#156979497">(Jan 27 2019 at 17:12)</a>:</h4>
<p>and then include both the context and the backtrace on the ctx?</p>



<a name="156979500"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/sleek%20derive%28Error%29/near/156979500" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Pietro Albini <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/sleek.20derive(Error).html#156979500">(Jan 27 2019 at 17:12)</a>:</h4>
<p>(name bikesheddable a lot)</p>



<a name="156979514"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/sleek%20derive%28Error%29/near/156979514" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Pietro Albini <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/sleek.20derive(Error).html#156979514">(Jan 27 2019 at 17:12)</a>:</h4>
<p>having to add the backtrace to every error is going to be a pain</p>



<a name="156979589"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/sleek%20derive%28Error%29/near/156979589" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/sleek.20derive(Error).html#156979589">(Jan 27 2019 at 17:15)</a>:</h4>
<blockquote>
<p>having to add the backtrace to every error</p>
</blockquote>
<p>This is something I need to get a better sense of. For example, people may not want a backtrace generated <em>for reasons</em>. Also, you might want a backtrace generated even if there isn't an underlying cause (actually, this seems like the <strong>more</strong> likely case)</p>



<a name="156979592"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/sleek%20derive%28Error%29/near/156979592" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/sleek.20derive(Error).html#156979592">(Jan 27 2019 at 17:15)</a>:</h4>
<p>since ideally the underlying error has its own backtrace</p>



<a name="156979638"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/sleek%20derive%28Error%29/near/156979638" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/sleek.20derive(Error).html#156979638">(Jan 27 2019 at 17:16)</a>:</h4>
<p>which includes the current libraries code location by construction</p>



<a name="156979647"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/sleek%20derive%28Error%29/near/156979647" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/sleek.20derive(Error).html#156979647">(Jan 27 2019 at 17:17)</a>:</h4>
<p>So bundling them together <em>seems</em> like a non-starter</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>